<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"></detail-nav-bar>

    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            @scroll="contentScroll">

      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo" @imgLoad="imageLoad"></detail-goods-info>
      <detail-param-info ref="params" :param-info="paramInfo"></detail-param-info>
      <detail-comment-info ref="comment" :comment-info="commentInfo"></detail-comment-info>
      <goods-list ref="recommend" :goods="recommends"></goods-list>
    </scroll>
    <detail-bottom-bar @addCart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backClick" v-show="isshow"/>
<!--    <toast message="哈哈哈"></toast>-->
  </div>
</template>

<script>
  import DetailNavBar from "./childComs/DetailNavBar";
  import detailSwiper from "./childComs/detailSwiper";
  import DetailBaseInfo from "./childComs/DetailBaseInfo";
  import DetailShopInfo from "./childComs/DetailShopInfo";
  import Scroll from "../../components/common/scroll/Scroll";
  import DetailGoodsInfo from "./childComs/DetailGoodsInfo";
  import DetailParamInfo from "./childComs/DetailParamInfo";
  import DetailCommentInfo from "./childComs/DetailCommentInfo";
  import GoodsList from "@/components/content/goods/GoodsList";
  import DetailBottomBar from "./childComs/DetailBottomBar";
  // import Toast from "../../components/common/toast/Toast";

  import {backTopMixin} from "../../common/mixin";
  import {itemListenerMinin} from "../../common/mixin";
  import { mapActions } from 'vuex'

  import {getDetail, Goods, Shop, GoodsParam, getRecommend} from "@/network/detail";
  import RecommendView from "../home/childComs/RecommendView";
  import {BACK_POSITION} from "../../common/const";

  export default {
    name: "Detail",
    components: {
      RecommendView,
      DetailNavBar,
      detailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      Scroll,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      GoodsList,
      DetailBottomBar,
      // Toast
    },
    data() {
      return {
        iid: null,
        topImages: [],
        goods: {},
        shop: {},
        paramInfo: {},
        detailInfo: {},
        commentInfo: {},
        recommends: [],
        themeTopY: [],
        positionY: 0,
        currentIndex: 0,
        message: ''
      }
    },
    mixins: [itemListenerMinin,backTopMixin],

    created() {
      this.iid = this.$route.params.iid
      getDetail(this.iid).then(res =>{
        //获取顶部的轮播图数据
        const data = res.result;
        this.topImages = data.itemInfo.topImages
      //  获取商品信息


        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

        //  创建商铺信息对象
        this.shop = new Shop(data.shopInfo)
      //  保存商品详情数据
        this.detailInfo = data.detailInfo
      //  获取参数信息
        this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)
        if(data.rate.cRate !== 0){
          this.commentInfo = data.rate.list[0]
        }
      })
      getRecommend().then(res =>{
        this.recommends = res.data.list
      })

      // this.$nextTick(()=>{
      //   this.themeTopY.push(0);
      //   this.themeTopY.push(this.$refs.params.$el.offsetTop);
      //   this.themeTopY.push(this.$refs.comment.$el.offsetTop);
      //   this.themeTopY.push(this.$refs.recommend.$el.offsetTop);
      //   console.log(this.themeTopY);
      // })
    },
    // updated() {
    //   this.themeTopY = []
    //   this.themeTopY.push(0);
    //   this.themeTopY.push(this.$refs.params.$el.offsetTop);
    //   this.themeTopY.push(this.$refs.comment.$el.offsetTop);
    //   this.themeTopY.push(this.$refs.recommend.$el.offsetTop);
    //   console.log(this.themeTopY);
    // },
    methods: {
      ...mapActions(['addCart']),
      //图片加载完成之后的操作
      imageLoad(){
        this.refresh()
        this.themeTopY.push(0);
        this.themeTopY.push(this.$refs.params.$el.offsetTop);
        this.themeTopY.push(this.$refs.comment.$el.offsetTop);
        this.themeTopY.push(this.$refs.recommend.$el.offsetTop);
      },
      titleClick(index){
        this.$refs.scroll.scrollTo(0,-this.themeTopY[index],300)
      },
      contentScroll(position){
        this.positionY = -position.y
        let length = this.themeTopY.length
        for(let i=0; i<length; i++){
          // if(this.positionY>this.themeTopY[i]&&this.positionY<this.themeTopY[i+1]){
          //   console.log(i);
          if(this.currentIndex !==i && ((i < length - 1 && this.positionY >=
            this.themeTopY[i] && this.positionY < this.themeTopY[i+1])||
            (i === length - 1 && this.positionY >= this.themeTopY[i]))){
            this.currentIndex = i
            console.log(this.currentIndex);
            this.$refs.nav.currentIndex = this.currentIndex
          }
        }
        this.isshow = (-position.y) > BACK_POSITION
      },
      addToCart(){
        const product = {}
        product.image = this.topImages[0]
        product.title = this.goods.title
        product.desc = this.goods.desc
        product.price = this.goods.realPrice
        product.iid = this.iid
        this.addCart(product).then(res =>{
          this.$toast.show(res,2000)
        })
        // this.addCart(product).then(res => {
        //   this.$toast.show(res , 2000)
        // })
        // this.$store.dispatch('addCart',product).then(res =>{
        //   console.log(res);
        // })
      }

    },
    destroyed() {
      this.$bus.$off('itemImgLoad',this.itemImgListener)
    }
  }
</script>

<style scoped>
 #detail{
   position: relative;
   z-index: 9;
   background-color: white;
   height: 100vh;
 }
 .detail-nav{
   position: relative;
   z-index: 9;
   background-color: white;
 }
  .content{
    height: calc(100% - 44px - 58px);
  }
</style>
